package com.example.wanandroid.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.wanandroid.R;
import com.example.wanandroid.core.bean.BaseData;
import com.example.wanandroid.core.bean.navigation.NavigationListData;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author jiangyi
 */
public class NavListAdapter extends RecyclerView.Adapter<NavListAdapter.NavListViewHolder> {
    List<BaseData> dataList = new ArrayList<>();
    private LayoutInflater mLayoutInflater;
    Context context;
    Object lock = new Object();

    public NavListAdapter(Context context){
        this.context = context;
        mLayoutInflater = LayoutInflater.from(context);
    }

    @NonNull
    @Override
    public NavListViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new NavListViewHolder(mLayoutInflater.inflate(R.layout.nav_detail_item,parent,false));
    }

    @Override
    public void onBindViewHolder(@NonNull NavListViewHolder holder, int position) {
        holder.textView.setText(((NavigationListData)(dataList.get(position))).getName());
    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }

    class NavListViewHolder extends RecyclerView.ViewHolder{
        TextView textView;

        public NavListViewHolder(@NonNull View itemView) {
            super(itemView);
            textView = itemView.findViewById(R.id.nav_name_text);
        }
    }

    public void addData(BaseData data){
        synchronized (lock){
            this.dataList.add(data);
            List<BaseData> list = this.dataList.stream().distinct().collect(Collectors.toList());
            this.dataList = list;
            notifyItemInserted(this.dataList.size()-1);
        }
    }

    public void addDataList(List<BaseData> data){
        synchronized (lock){
            this.dataList.addAll(data);
            List<BaseData> list = this.dataList.stream().distinct().collect(Collectors.toList());
            this.dataList = list;
            notifyDataSetChanged();
        }
    }
}
